package com.rfid.afd.activity;

import android.app.Activity;
import android.content.Context;
import android.content.Intent;
import android.os.Bundle;
import android.view.View;
import android.view.ViewGroup;
import android.widget.TextView;

import androidx.annotation.NonNull;
import androidx.annotation.Nullable;
import androidx.recyclerview.widget.DividerItemDecoration;
import androidx.recyclerview.widget.GridLayoutManager;
import androidx.recyclerview.widget.RecyclerView;

import com.rfid.rls_psw.App;
import com.rfid.rls_psw.R;
import com.rfid.util.RfidManager;
import com.rfid.util.UserManager;

import java.util.ArrayList;
import java.util.List;

import butterknife.BindView;
import butterknife.ButterKnife;

public class HomeActivity extends Activity {

    @BindView(R.id.recyclerview)
    RecyclerView mRecyclerView;
    @BindView(R.id.name)
    TextView mName;
    @BindView(R.id.nickName)
    TextView mNickName;

    public static void startActivity(Context context) {
        Intent intent = new Intent();
        intent.setClass(context,HomeActivity.class);
        context.startActivity(intent);
    }

    @Override
    protected void onCreate(@Nullable Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_home);
        ButterKnife.bind(this);
        mRecyclerView.setLayoutManager(new GridLayoutManager(this,2,RecyclerView.VERTICAL,false));
        mRecyclerView.addItemDecoration(new DividerItemDecoration(this,RecyclerView.VERTICAL));
        List<Action> actions = new ArrayList<>();
        actions.add(new Action(R.string.locate,LocateActivity.class,true));
        actions.add(new Action(R.string.checkbook, CheckActivity.class,true));
        actions.add(new Action(R.string.grounding,ShangjiaActivity.class,true));
//        actions.add(new Action(R.string.undercarriage,null));
        actions.add(new Action(R.string.delete,DeleteActivity.class,true));
//        actions.add(new Action(R.string.bookquery,null));
        actions.add(new Action(R.string.look, SearchBookActivity.class,true));
        actions.add(new Action(R.string.statistics,StatisticsActivity.class,true));
        actions.add(new Action(R.string.makelaymark,SetLabelActivity.class,true));
//        actions.add(new Action(R.string.pick,null));
        actions.add(new Action(R.string.update,SyncActivity.class,true));
//        actions.add(new Action(R.string.moveshelft,null));
        actions.add(new Action(R.string.aloneshelft,ShunjiaActivity.class,true));
        actions.add(new Action(R.string.pick,GatherActivity.class,true));
        mRecyclerView.setAdapter(new MyAdapter(actions));
        RfidManager.getInstance().connect(this);

        mName.setText("用户名：" + UserManager.getInstance().getLoginUser().getName());
        mNickName.setText("昵称：" +UserManager.getInstance().getLoginUser().getNickName());
    }

    @Override
    protected void onDestroy() {
        super.onDestroy();
        RfidManager.getInstance().quit();
    }

    class MyAdapter extends RecyclerView.Adapter<MyAdapter.ViewHolder> {
        private List<Action> mActions;
        public MyAdapter(List<Action> actions) {
            mActions = actions;
        }

        @NonNull
        @Override
        public ViewHolder onCreateViewHolder(@NonNull ViewGroup parent, int viewType) {
            return new ViewHolder(getLayoutInflater().inflate(R.layout.home_item,parent,false));
        }

        @Override
        public void onBindViewHolder(@NonNull ViewHolder holder, int position) {
            holder.textView.setText(mActions.get(position).lable);
            holder.itemView.findViewById(R.id.cardView).setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View v) {
                    mActions.get(position).action();
//                    ScanStratigraphyActivity.startActivity(HomeActivity.this);
//                    RfidDecoder decoder = new RfidDecoder();
//                    String hex = "E200001918190052195017B6";//010200A118030000C4000000,E200001918190052195017B6
//                    hex = hex.substring(4 * 2, hex.length());
//                    System.out.println("hex = " + hex);
//                    byte data[] = new byte[15];
//                    Util.hexStr2bytes(hex,data);
//                    for(int i=0;i<data.length;i++) {
//                        System.out.println((char) data[i]);
//                    }
//                    int r = decoder.iRFID_Decode96bit(data);
//                    System.out.println(new String(data).trim());
                }
            });
        }

        @Override
        public int getItemCount() {
            return mActions.size();
        }

        class ViewHolder extends RecyclerView.ViewHolder{
            public TextView textView;
            public ViewHolder(@NonNull View itemView) {
                super(itemView);
                textView = itemView.findViewById(R.id.text);
            }
        }
    }

    class Action {
        int lable;
        Class clazz;
        boolean skip;
        public Action(int lable,Class clazz) {
            this.lable = lable;
            this.clazz = clazz;
        }
        public Action(int lable,Class clazz,boolean skip) {
            this.lable = lable;
            this.clazz = clazz;
            this.skip = skip;
        }

        public void action() {
            if(clazz == null) {
                return;
            }
            if(skip) {
                Intent intent = new Intent();
                intent.setClass(App.getInstance(),clazz);
                intent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
                App.getInstance().startActivity(intent);
                return;
            }
//            Intent intent = new Intent(App.getInstance(),clazz);
//            intent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
//            App.getInstance().startActivity(intent);
            ScanStratigraphyActivity.startActivity(App.getInstance(),clazz);
        }
    }

}
